import QtQuick 1.1
import com.nokia.symbian 1.1
import com.nokia.extras 1.1


import QtQuick 1.0

import "core.js" as Core
import "settings.js" as Settings
import "date.js" as CustDate

Page {
    id: detailsView
    property int fuelId

    property string currency;
    property string viewType;
    property string date;

    TitleHeader {
        id: header
        text: "Fuel Details"

    }

    Grid {
        id: grid1
        x: 33
        y: 131
        width: 295
        height: 343
        smooth: true
        spacing: 4
        z: 1
        rows: 8
        columns: 1


        Text {
            id: text3
            x: 67
            y: 123
            color: platformStyle.colorNormalLight
            text: qsTr("Cost ")+"("+currency+")"
            font.pixelSize: 18
        }

        TextField {
            id: textCost

            x: 109
            y: 60
            width: 300
            height: 40
            text: "fuelCost"
            maximumLength: 7
        }

        Text {
            id: text2
            x: 65
            y: 129
            color: platformStyle.colorNormalLight
            text: qsTr("Quantity (Liter)")
            font.pixelSize: 18
        }

        TextField {
            id: textQty

            x: 103
            y: 53
            width: 300
            height: 40
            text: "fuelQty"
            maximumLength: 7
        }

        Text {
            id: text4
            x: 65
            y: 117
            color: platformStyle.colorNormalLight
            text: qsTr("Note")
            font.pixelSize: 18
        }

        TextField {
            id: textNote
            x: 103
            y: 61
            width: 300
            height: 40
            text: "Note"
        }

        Text {
            id: text5
            x: 60
            y: 125
            color: platformStyle.colorNormalLight
            text: qsTr("Date")
            font.pixelSize: 18
        }

        SelectionListItem {
            id: selectionlistitemDate
            x: 26
            y: 331
            implicitWidth: 300
            //title: "Today"
            Component.onCompleted: {
                var d = new Date(date);

               title =  CustDate.dateFormat(d, "mmm d, yyyy");


            }

            onClicked: {
                var d = new Date(date);
                console.debug(date);
                 tDialog.year = d.getFullYear();
                 tDialog.month = d.getMonth()+1;
                tDialog.day = d.getDate();
                 tDialog.open()
            }//dateDlg.open()
        }
    }


    DatePickerDialog {
        id: tDialog
        titleText: "Set Date"
        acceptButtonText: "OK"
        //rejectButtonText:"Cancel"


        onAccepted: {
            var strDt = tDialog.month+"/"+tDialog.day+"/"+tDialog.year;
            var d = new Date(strDt);
            selectionlistitemDate.title =  CustDate.dateFormat(d, "mmm d, yyyy");
        }



    }


    BorderImage {
        id: border_image1
        x: -13
        y: 41
        width: 360
        height: 545
        source: "bkg2.png"
    }
    tools: ToolBarLayout {
        ToolButton {iconSource: "toolbar-back"; onClicked: pageStack.pop()}

        Button { id: editTool; text: "Save"; onClicked: {update()} }

        ToolButton { id: deleteTool; iconSource: "toolbar-delete";
            onClicked: {alertEx.msg="Do you want to delete the record ?"; alertEx.open()}}


    }
    AlertDialogEx{
        id:alertEx
        onAccepted: {deleteItem(); pageStack.pop();}
    }

    onStatusChanged: {
        var item
        if(status == PageStatus.Activating) {
            Settings.initialize();
            viewType = Settings.getSetting("viewType");
            currency = Settings.getSetting("currency");

            if(viewType === "0")
                viewType = "All Time"
            if(currency === "0")
                currency = "USD"

            item = Core.readFuelItem(fuelId);
            //textTitle.text = item.title;
            textQty.text = item.qty;
            textCost.text = item.cost;
            textNote.text = item.note;
            selectionlistitemDate.title = item.date;
            date = item.date;

        }
    }
    function deleteItem()
    {
        Core.deleteFuelItem(fuelId);
    }

    function isNumber (o) {
      return ! isNaN (o-0);
    }


    AlertDialog{
        id:alert
    }

    function update()
    {
        if(isNumber(textCost.text) == true && isNumber(textQty.text) == true )
        {
        var item = Core.readFuelItem(fuelId);


        item.note = textNote.text;
        item.cost = textCost.text;
        item.qty = textQty.text;
        item.date = selectionlistitemDate.title;
        Core.updateFuelItem(item);
            alert.msg ="Record saved Successfully !";
            alert.open();
        }else
        {
            alert.msg = "Please enter valid data!";
            alert.open();
        }
    }

    function loadSettings()
    {
        Settings.initialize();
        var temp = Settings.getSetting("viewType");
        if(temp === "0")
        {
            viewType = "All Time";
        }
        else
        {
            viewType = temp;
        }

        temp = Settings.getSetting("currency");
        if(temp === "0")
        {
            currency = "USD";
        }
        else
        {
            currency = temp;
        }



    }

}
